//
//  AIGirlModelDetailBottomView.swift
//  AIGirl
//
//  Created by Rae on 2024/1/2.
//

import UIKit

class AIGirlModelDetailBottomView: UIView {

    private var shadowView = UIView()
    private lazy var likeButton = c_make_ex(UIButton.self)
        .image("AIGirl_Discover_Detail_unlike").image("AIGirl_Discover_Detail_like", state: .selected).end()
     lazy var getButton = c_make_ex(UIButton.self)
        .image("AIGirl_Discover_Detail_diamond").bgImage("AIGirl_Button_bg")
        .bgImage("AIGirl_Discover_Detail_VIPBtn", state: .selected).corner(20)
        .color(.white).font(20.font_bold).color("#643208", state: .selected)
        .end()
    
    var model: AIGirlItemModel?{
        didSet{
            guard let md = model else { return }
            let coin = "\(md.coin) Get"
            getButton.setTitle(coin, for: .normal)
            isVip = md.isVip == 1
            getButton.isSelected = md.isVip == 1
        }
    }
    
    var detailModel: AIGirlDiscoverDetailModel?{
        didSet{
            guard let md = detailModel else { return }
            likeButton.isSelected = md.isCollect
        }
    }
    
//    var picModel:AIGirlDiscoverDetailPicModel?{
//        didSet {
//            guard let md = picModel else { return }
//            let coin = "\(md.lookPrice) Get"
//             getButton.setTitle(coin, for: .normal)
//        }
//    }
    
    var isVip: Bool = false
    
    var likeClosure: ((_ likeState: Bool) -> Void)?
    var getClosure: ((_ purchaseModel: AIGirlItemModel) -> Void)?
    var vipClosure: (() -> Void)?
    var bugCoinClosure: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            shadowView
            likeButton
            getButton
        }
        
        shadowView.fillContainer()
        likeButton.leading(30).size(50).top(39)
        getButton.trailing(30).height(50).top(38)
        getButton.Leading == likeButton.Trailing + 15
        
        likeButton.addTarget(self, action: #selector(clickLikeButton), for: .touchUpInside)
        getButton.addTarget(self, action: #selector(clickGetButton), for: .touchUpInside)
    }
    
    @objc private func clickGetButton(){
        if isVip {
            if GlobalTool.userModel?.isVip.intValue ?? 0 > 0 {
                getModel()
            }else{
                vipClosure?()
            }            
        }else{
            getModel()
        }
    }
    
    private func getModel(){
        guard let purchaseModel = model else { return }
        if GlobalTool.userModel?.money ?? 0 < purchaseModel.coin {
            bugCoinClosure?()
        }else{
            getClosure?(purchaseModel)
        }
    }
    
    @objc private func clickLikeButton(){
        
        likeButton.isSelected = !likeButton.isSelected
        likeClosure?(likeButton.isSelected)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        shadowView.fj.gradientColors(.topToBottom(["#111012".toColor.alpha(0),"#151417".toColor]))
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
